package com.soft.javase.exam;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/17
 */
public class HashSetDemo {
    private static final String CHAR =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
            "abcdefghijklmnopqrstuvwxyz" +
            "0123456789" +
            "!@#$%^&*()_+-=[]{}|;':\",./<>?";

    public static void main(String[] args) {
        String[] arr = new String[200];
        Random random = new Random();

        for (int i = 0; i < arr.length; i++) {
            arr[i] = randomString(2, random);
        }

        Set<String> unique = new HashSet<>();
        Set<String> duplicates = new HashSet<>();

        for (String s : arr) {
            if (!unique.add(s)) {
                duplicates.add(s);
            }
        }

        System.out.println("重复的字符串有 " + duplicates.size() + " 种：" + duplicates);
    }

    private static String randomString(int length, Random random) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHAR.length());
            sb.append(CHAR.charAt(index));
        }
        return sb.toString();
    }
}
